use super::register_parsers::register;
use super::Token;
use nom::digit;
use nom::types::CompleteStr;

named!(pub integer_operand<CompleteStr, Token>,
    ws!(
        do_parse!(
            tag!("#") >>
            num: digit >>
            (
                Token::IntegerOperand{
                    value: num.parse::<i32>().unwrap()
                }
            )
        )
    )
);

named!(pub operand<CompleteStr, Token>,
    do_parse! (
        op: alt!(register | integer_operand) >> (op)
    )
);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_integer_operand() {
        // Test a valid integer operand
        let result = integer_operand(CompleteStr("#10"));
        assert_eq!(result.is_ok(), true);
        let (rest, value) = result.unwrap();
        assert_eq!(rest, CompleteStr(""));
        assert_eq!(value, Token::IntegerOperand { value: 10 });

        // Test an invalid one (missing the #)
        let result = integer_operand(CompleteStr("10"));
        assert_eq!(result.is_ok(), false);
    }
}
